<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;

class ReservationComment extends Model
{
    use SoftDeletes;
    protected $table = 'reservation_comment';

    /**
     * 定义主键
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 禁止批量赋值的字段
     *
     * @var array
     */
    protected $guarded = ['id','created_at','deleted_at'];

    /**
     * 软删除应该被调整为日期的属性
     * [$dates description]
     * @var [type]
     */
    protected $dates=[
        'created_at', 'updated_at','deleted_at'
    ];

    /*追加字段*/
    protected $appends = ['imgs_url','video_img_url','video_url','zaned','followed','reply_user_name'];

    /**
     * 关联回复用户
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function replyer()
    {
        return $this -> hasOne(User::class,'id','reply_user_id');
    }

    //给图片添加链接
    public function getImgsUrlAttribute()
    {
        $newarr = [];
        if($this -> attributes['imgs']){
            $arr = json_decode($this -> attributes['imgs']);
            foreach($arr as $v){
                $newarr[] = config('app.img_url') . $v;
            }
        }
        return $newarr;
    }


    public function getVideoImgUrlAttribute()
    {
        if($this -> attributes['video_img']){
            return config('app.img_url') . $this -> attributes['video_img'];
        }
        return '';
    }

    public function getVideoUrlAttribute()
    {
        if($this -> attributes['video']){
            return config('app.img_url') . $this -> attributes['video'];
        }
        return '';
    }


    public function commenter()
    {
        return $this -> hasOne(User::class,'id','user_id');
    }


    // 点赞
    public function getZanedAttribute()
    {
        if(!$user_id = auth() -> id()){
            return false;
        }
        $where = [
            'reservation_id' => $this -> reservation_id,
            'operate_type'   => 1,
            'user_id'        => $user_id,
            'obj_id'         => $this -> id,
        ];

        return ReservationOperateLogs::where($where) -> first()
            ? true : false;

    }

    public function getFollowedAttribute()
    {
        if(!$user_id = auth() -> id()){
            return false;
        }


        return $followed = UserFollow::where('follow_user_id',$this -> user_id) -> where('user_id',$user_id) -> where('is_admin',0) -> first()
            ? true : false;
    }



    // 回复用户的id
    public function getReplyUserNameAttribute()
    {
        $user = User::find($this -> reply_user_id);
        return $this -> reply_user_id ? ($user ? ($user -> real_name ?: $user -> user_name) : null) : null;
    }

    /*
     * 存储预约评论内容
     * @return array
     */
    public static function store($param){

        if($param['imgs']){
            $arr = [];
            foreach(explode(',',$param['imgs']) as $v){
                $arr[] = $v;
            }
            $param['imgs'] = json_encode($arr);
        }
        if($param['reply_id']){

            $param['reply_user_id'] = self::where('id',$param['reply_id']) -> value('user_id') ;
        }else{
            $param['reply_id'] = 0 ;
        }
        $param['user_id']   = auth() -> id();
        $param['is_expert'] = (auth() -> user() -> role == 2) ? 1 : 0;

        try{
            $res1 = 1;
            DB::beginTransaction();

            auth() -> user() -> role == 2 && $res1 = Reservation::where('id',$param['reservation_id']) -> update(['expert_comment_state' => 1]);
            $res2 = self::create($param);

            if($res1 && $res2){

                DB::commit();
                return $res2;
            }else{
                DB::rollBack();
                return false;
            }
        }catch(\Exception $e){
            DB::rollBack();
            return ['error' => 4043];
        }


    }
}
